package com.kuangstudy.service.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuangstudy.entity.Collect;
import com.kuangstudy.entity.CollectCategory;
import com.kuangstudy.mapper.CollectMapper;
import com.kuangstudy.service.collectcategory.ICollectCategoryService;
import com.kuangstudy.vo.CollectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: yykk Administrator
 * Version: 1.0
 * Create Date Time: 2021/12/1 20:34.
 * Update Date Time:
 *
 * @see
 */
@Component("collectDbService")
@Slf4j
public class CollectDbServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    @Autowired
    private ICollectCategoryService collectCategoryService;

    @Override
    public List<CollectVo> loadCollectVos() {
        log.info("--------------> collectDbService");
        // 1: 查询所有的分类
        List<CollectCategory> collectCategories = collectCategoryService.list();
        // 2：根据分类id查询对应数据
        List<CollectVo> collectVoList = new ArrayList<>();
        for (CollectCategory collectCategory : collectCategories) {
            // 3： 查询表中所有的收藏夹的数据
            LambdaQueryWrapper<Collect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Collect::getCategoryId, collectCategory.getId());
            lambdaQueryWrapper.eq(Collect::getStatus, 1);
            List<Collect> collectList = this.list(lambdaQueryWrapper);
            // 4：把需要的数据放入到CollectVo进行返回
            CollectVo collectVo = new CollectVo();
            collectVo.setCollectList(collectList);
            collectVo.setCategoryId(collectCategory.getId());
            collectVo.setCategoryTitle(collectCategory.getCategoryTitle());
            collectVoList.add(collectVo);
        }
        // 6: 返回即可
        return collectVoList;
    }


}


